﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Xml;

namespace Common
{
    public class PrintPageBase : System.Web.UI.Page
    {
        private static string path = Path.GetDirectoryName(Path.GetDirectoryName(HttpContext.Current.Server.MapPath("/")));
        protected internal PModel.PConfig config = PBLL.PConfig.GetModel();
        public PrintPageBase() { }

        public void GetAd(System.Web.UI.WebControls.Repeater rpt, string type)
        {
            PBLL.PAd bll = new PBLL.PAd();
            string where = "AType='印刷项目'";
            if (bll.Exists("AType='" + type + "'"))
            {
                where = "AType='" + type + "'";
            }
            rpt.DataSource = bll.GetList(where);
        }

        /// <summary>
        /// 遮罩提示窗口
        /// </summary>
        /// <param name="w">宽度</param>
        /// <param name="h">高度</param>
        /// <param name="msgtitle">窗口标题</param>
        /// <param name="msgbox">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        protected void JscriptMsg(int w, int h, string msgtitle, string msgbox, string url, string msgcss)
        {
            string msbox = "";
            msbox += "<script type=\"text/javascript\">\n";
            msbox += "parent.jsmsg(" + w + "," + h + ",\"" + msgtitle + "\",\"" + msgbox + "\",\"" + msgcss + "\");location.href='" + url + "';\n";
            msbox += "</script>\n";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsMsg", msbox);
        }

        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        protected void JscriptPrint(string msgtitle, string url, string msgcss)
        {
            string msbox = "";
            msbox += "<script type=\"text/javascript\">\n";
            msbox += "parent.jsprint(\"" + msgtitle + "\",\"" + msgcss + "\");location.href='" + url + "';\n";
            msbox += "</script>\n";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox);
        }

        #region
        ///// <summary>
        ///// 返回伪静态统一链接地址
        ///// </summary>
        //protected string URLRewrite(string _key, params object[] _params)
        //{
        //    Hashtable _siteurls = loadConfig(path + "\\" + ConfigurationManager.AppSettings["urlConfig"].ToString());
        //    PModel.PUrl model = _siteurls[_key] as PModel.PUrl;
        //    if (model == null)
        //    {
        //        return "";
        //    }
        //    try
        //    {
        //        string _rewriteurl = string.Format(model.Path, _params);
        //        if (config.P_ReWrite == 1)
        //        {
        //            return config.P_WebPath.TrimEnd('/') + _rewriteurl;
        //        }
        //        string _originalurl = model.Page;
        //        if (!string.IsNullOrEmpty(model.Query))
        //        {
        //            _originalurl = model.Page + "?" + Regex.Replace(_rewriteurl, model.Pattern, model.Query, RegexOptions.None | RegexOptions.IgnoreCase);
        //        }
        //        return config.P_WebPath.TrimEnd('/') + _originalurl;
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        //public Hashtable loadConfig(string urlFilePath)
        //{
        //    //从缓存中根据键读取，并使用as转换
        //    Hashtable Cache_Siteurl = HttpContext.Current.Cache["Cache_Siteurl"] as Hashtable;
        //    if (Cache_Siteurl == null)
        //    {
        //        //创建缓存依赖项
        //        CacheDependency dependency = new CacheDependency(urlFilePath);
        //        //创建缓存
        //        Hashtable ht = new Hashtable();

        //        XmlDocument xml = new XmlDocument();
        //        xml.Load(urlFilePath);

        //        XmlNode root = xml.SelectSingleNode("urls");
        //        foreach (XmlNode n in root.ChildNodes)
        //        {
        //            if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "rewrite")
        //            {
        //                XmlAttribute name = n.Attributes["name"];
        //                XmlAttribute path = n.Attributes["path"];
        //                XmlAttribute page = n.Attributes["page"];
        //                XmlAttribute querystring = n.Attributes["querystring"];
        //                XmlAttribute pattern = n.Attributes["pattern"];

        //                if (name != null && path != null && page != null && querystring != null && pattern != null)
        //                {
        //                    ht.Add(name.Value, new PModel.PUrl(name.Value, path.Value, pattern.Value, page.Value.Replace("^", "&"), querystring.Value.Replace("^", "&")));
        //                }
        //            }
        //        }
        //        HttpContext.Current.Cache.Add("Cache_Siteurl", ht, dependency, Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0), CacheItemPriority.Default, null);
        //        Cache_Siteurl = HttpContext.Current.Cache["Cache_Siteurl"] as Hashtable;
        //    }

        //    return Cache_Siteurl;
        //}
        #endregion
    }
}
